package com.suisung.shopsuite.trade.controller.front;


import cn.hutool.core.bean.BeanUtil;
import com.suisung.shopsuite.common.utils.ContextUtil;
import com.suisung.shopsuite.core.web.CommonRes;
import com.suisung.shopsuite.core.web.controller.BaseController;
import com.suisung.shopsuite.pay.model.req.WithdrawAddReq;
import com.suisung.shopsuite.trade.model.input.PosterInput;
import com.suisung.shopsuite.trade.model.output.PosterOutput;
import com.suisung.shopsuite.trade.model.req.DistributionOrderListReq;
import com.suisung.shopsuite.trade.model.req.PosterReq;
import com.suisung.shopsuite.trade.model.res.DistributionOrderRes;
import com.suisung.shopsuite.trade.service.DistributionOrderService;
import com.suisung.shopsuite.trade.service.DistributionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "分销用户信息")
@RestController
@RequestMapping("/front/trade/distribution")
public class DistributionController extends BaseController {
    @Autowired
    private DistributionOrderService distributionOrderService;

    @Autowired
    private DistributionService distributionService;

    @ApiOperation(value = "用户分销数据概况", notes = "用户分销数据概况")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public CommonRes<?> index() {
        Integer userId = ContextUtil.checkLoginUserId();

        Map index = distributionService.index(userId);

        return success(index);
    }

    @ApiOperation(value = "推广中心", notes = "推广中心")
    @RequestMapping(value = "/getCommissionTimeline", method = RequestMethod.GET)
    public CommonRes<?> getCommissionTimeline() {
        Integer userId = ContextUtil.checkLoginUserId();

        return success(distributionService.getCommissionTimeline(userId));
    }

    @ApiOperation(value = "用户分销订单列表数据", notes = "用户分销订单列表数据")
    @RequestMapping(value = "/listsOrder", method = RequestMethod.GET)
    public CommonRes<DistributionOrderRes> listsOrder(DistributionOrderListReq distributionOrderListReq) {
        Integer userId = ContextUtil.checkLoginUserId();
        distributionOrderListReq.setUserId(userId);
        DistributionOrderRes distributionOrderRes = distributionOrderService.listsOrder(distributionOrderListReq);

        return success(distributionOrderRes);
    }


    @ApiOperation(value = "粉丝及佣金", notes = "粉丝及佣金")
    @RequestMapping(value = "/listsCommission", method = RequestMethod.GET)
    public CommonRes<?> listsCommission(
            @RequestParam(name = "ugc_level") @ApiParam("分销等级:1-一级，2-二级， 3-三级") Integer ugcLevel,
            @RequestParam(name = "page", defaultValue = "1") @ApiParam("当前页码") Integer page,
            @RequestParam(name = "size", defaultValue = "20") @ApiParam("行数") Integer size
    ) {
        Integer userId = ContextUtil.checkLoginUserId();
        return success(distributionService.listsCommission(userId, ugcLevel, page, size));
    }

    @ApiOperation(value = "粉丝及佣金", notes = "粉丝及佣金")
    @RequestMapping(value = "/getCommissionNum", method = RequestMethod.GET)
    public CommonRes<?> getCommissionNum() {
        Integer userId = ContextUtil.checkLoginUserId();
        return success(distributionService.getCommissionNum(userId));
    }

    @ApiOperation(value = "佣金转余额详情", notes = "佣金转余额详情")
    @RequestMapping(value = "/withdraw", method = RequestMethod.GET)
    public CommonRes<?> withdraw() {
        Integer userId = ContextUtil.checkLoginUserId();

        return success(distributionService.withdraw(userId));
    }

    @ApiOperation(value = "佣金转余额", notes = "佣金转余额")
    @RequestMapping(value = "/doWithdraw", method = RequestMethod.GET)
    public CommonRes<?> doWithdraw() {
        Integer userId = ContextUtil.checkLoginUserId();

        return success(distributionService.doWithdraw(userId));
    }

    @ApiOperation(value = "佣金提现", notes = "佣金提现")
    @RequestMapping(value = "/doWithdrawMoney", method = RequestMethod.POST)
    public CommonRes<?> doWithdrawMoney(WithdrawAddReq withdrawAddReq) {
        Integer userId = ContextUtil.checkLoginUserId();
        withdrawAddReq.setUserId(userId);

        return success(distributionService.doWithdrawMoney(withdrawAddReq));
    }

    @ApiOperation(value = "余额提现", notes = "余额提现")
    @RequestMapping(value = "/balanceCashOut", method = RequestMethod.POST)
    public CommonRes<?> balanceCashOut(WithdrawAddReq withdrawAddReq) {
        Integer userId = ContextUtil.checkLoginUserId();
        withdrawAddReq.setUserId(userId);

        return success(distributionService.balanceCashOut(withdrawAddReq));
    }

    @ApiOperation(value = "推广海报", notes = "推广海报")
    @RequestMapping(value = "/poster", method = {RequestMethod.POST, RequestMethod.GET})
    public CommonRes<PosterOutput> poster(PosterReq req) {
        Integer userId = ContextUtil.checkLoginUserId();
        PosterInput input = BeanUtil.copyProperties(req, PosterInput.class);
        input.setUserId(userId);

        return success(distributionService.poster(input));
    }
}
